(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:loginDialog
     * @description
     * # loginDialog
     */
    angular.module('webApp')
        .run(["$templateCache", "dataService",
            function($templateCache, dataService) {
                var tplName = "views/directives.selectsupplierlist.html";
                dataService.tpl(tplName).then(function(html) {
                    $templateCache.put(tplName, html);
                });
            }
        ])
        .directive('selectSupplierList', ["$rootScope", "helper", function($rootScope, helper) {
            return {
                restrict: 'EA',
                template: "<span><a ng-show='!stype' href='javascript:;' class='sui-btn btn-link' ng-click='showPro()'>+ {{supplier.default_admin ||'选择供应商'|translate}}</a><span ng-show='stype' >{{supplier.default_admin}}</span></span>",
                require: 'ngModel',
                scope: {
                    ngModel: "=",
                    stype: "@",
                    callback: "&",
                },
                controller: ['$scope', 'authService', 'sessionService', 'dataService', '$compile', 'helper', '$templateCache', '$timeout', function($scope, authService, sessionService, dataService, $compile, helper, $templateCache, $timeout) {
                    $scope.productcat = {};
                    $scope.showPro = function() {
                        $scope.initData().then(showSelect)
                    }
                    $scope.initData = function(pageNo) {
                        pageNo = pageNo ? pageNo : 1;
                        $scope.page_size = 10;
                        var query = { page_no: pageNo, page_size: $scope.page_size };
                        if ($scope.company_id) {
                            query.query = { "company_id": $scope.pid };
                        }
                        return dataService.sellerSuppliersGet(query).then(function(r) {
                            // console.log("rrrrrrrrrrrrrrrrr===",r);
                            if (!r.Msg) {
                                $scope.suppliers = r.company;
                                $scope.supplierTotal = r.total_count;
                            }
                        });
                    }
                    $scope.addSuppliers = function(supplier) {
                        $scope.supplier = supplier;
                    }

                    function showSelect() {
                        $scope.error = null;
                        //弹出
                        $.alert({
                            backdrop: "static",
                            keyboard: true,
                            title: '{{"Supplier"|translate}}',
                            body: $templateCache.get("views/directives.selectsupplierlist.html"),
                            okBtn: '{{"ok"|translate}}',
                            closeBtn: true,
                            transition: true,
                            width: 'large',
                            height: 300,
                            show: function(e) {
                                $compile($(e.currentTarget).contents())($scope);
                            },
                            okHide: function(e) {
                                $timeout(function() {
                                    if (_.size($scope.supplier) > 0) {
                                        $scope.callback({ supplier: $scope.supplier });
                                    }
                                })
                            },
                            shown: function(e) {}
                        });
                    }

                    $scope.initProductcat = function(id) {
                        $timeout(function() {
                            dataService.sellerSuppliersOne(id).then(function(suppliers) {
                                $scope.supplier = suppliers;
                                // $scope.stype    = 1;
                                console.log(" $scope.ngModel", $scope.ngModel);
                                console.log(" $scope.supplier", $scope.supplier);
                            });
                        });
                    }
                }],
                link: function(scope, elem, attrs, ctr) {
                    scope.$watch('ngModel', function(newValue, oldValue, scope) {
                        console.log("11111111111111111111111111111==================", newValue, oldValue);
                        if (newValue) {
                            scope.initProductcat(newValue);
                        }
                    });
                }
            }
        }]);
})();
